
package bienvenido;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import javax.bluetooth.*;

public class ServicesSearch {

    static final UUID OBEX_OBJECT_PUSH = new UUID (0x1105);
    public static final Vector/*<String>*/ serviceFound = new Vector();
    public static int largo_servicios;   
    public static final Vector/*<String>*/ direcDispositivos = new Vector();
    public static final Vector/*<String>*/ nickDispositivos = new Vector();
    
    public static void servicios(String[] args) throws IOException, InterruptedException {

        //Primero corre RemoteDeviceDiscovery y utiliza los dispositivos encontrados
        RemoteDeviceDiscovery.detector(null);
        largo_servicios = 0;
        serviceFound.clear();

        UUID serviceUUID = OBEX_OBJECT_PUSH;
        if ((args != null) && (args.length > 0)) {
            serviceUUID = new UUID(args[0], false);
        }
        final Object serviceSearchCompletedEvent = new Object();

        DiscoveryListener listener = new DiscoveryListener() {

            public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
            }

            public void inquiryCompleted(int discType) {
            }

            public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
                for (int i = 0; i < servRecord.length; i++) {
                    String disp = servRecord[i].getHostDevice().getBluetoothAddress();
                    String nick="";
                    try {
                        nick = servRecord[i].getHostDevice().getFriendlyName(false);
                    } catch (IOException cantGetDeviceName) {
                    }
                    direcDispositivos.add(disp);
                    nickDispositivos.add(nick);
                    String url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
                    if (url == null) {
                        continue;                      
                    }
                    serviceFound.add(url);
                    largo_servicios++;
                    DataElement serviceName = servRecord[i].getAttributeValue(0x0100);
                    if (serviceName != null) {
                        System.out.println("Servicio " + serviceName.getValue() + " encontrado: " + url);
                    } else {
                        System.out.println("Servicio encontrado: " + url);
                    }
                }
            }

            public void serviceSearchCompleted(int transID, int respCode) {
                System.out.println("Búsqueda de servicios completada!");
                synchronized(serviceSearchCompletedEvent){
                    serviceSearchCompletedEvent.notifyAll();
                }
            }
        };
        UUID[] searchUuidSet = new UUID[] { serviceUUID };
        int[] attrIDs =  new int[] {
                0x0100 // Nombre servicio
        };
        for(Enumeration en = RemoteDeviceDiscovery.devicesDiscovered.elements(); en.hasMoreElements(); ) {
            RemoteDevice btDevice = (RemoteDevice)en.nextElement();

            synchronized(serviceSearchCompletedEvent) {
                System.out.println("Buscando servicios en " + btDevice.getBluetoothAddress() + " " );
                LocalDevice.getLocalDevice().getDiscoveryAgent().searchServices(attrIDs, searchUuidSet, btDevice, listener);
                serviceSearchCompletedEvent.wait();
            }
        }
    }
}